﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using Bainx.Extend;
using Bainx.WPF.Extend;
using System.Threading;
using System.Windows.Threading;
using  Bainx.WPF.Extend.Struct;

namespace WM360Client
{
    /// <summary>
    /// SuspendWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SuspendWindow : WPFBaseWindow
    {
        /// <summary>
        /// 调用者窗体
        /// </summary>
        Window parentWindow;
        /// <summary>
        /// 获取或设置调用者窗体(父窗体)
        /// </summary>
        public Window ParentWindow
        {
            get { return parentWindow; }
            set { parentWindow = value; }
        }

        private MenuSource[] menuSources;
        //private string[] menuHeader = new string[] { "隐藏主窗体", "隐藏悬浮窗", "状态", "退出" };
        
        

        public SuspendWindow()
        {
            InitializeComponent();


            this.ShowInTaskbar = false;
            this.Left = System.Windows.SystemParameters.FullPrimaryScreenWidth-this.Width-100;
            this.Top = 100;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            //this.MinimizeBox = System.Windows.Visibility.Collapsed;
            //this.MaximizeBox = System.Windows.Visibility.Collapsed;
            //this.CloseBox = System.Windows.Visibility.Collapsed;
            this.HeadArea = System.Windows.Visibility.Collapsed;
            this.FootArea = System.Windows.Visibility.Collapsed;
            this.MouseMove += new MouseEventHandler(DragMoveWindow);
            

           // MenuItem tmp = new MenuItem();
            


           // ContextMenu contextMenu = new ContextMenu();

            
           // contextMenu.ItemsSource = this.ContentMenu.Items;
           // contextMenu.Background = new LinearGradientBrush(Color.FromArgb(192, 192, 192, 240), Color.FromArgb(128, 240, 240, 255), 315);
           // contextMenu.BorderThickness = new Thickness(1);
           // contextMenu.BorderBrush = new SolidColorBrush(Color.FromArgb(128, 64, 64, 192));

           // //DropShadowEffect effect = new DropShadowEffect();
           // //effect.BlurRadius = 10;
           // //effect.Direction = 0;
           // //effect.ShadowDepth = 0;
           // //effect.Opacity = 0;
           // //effect.Color = Color.FromArgb(0, 255, 255, 255);
           // //contextMenu.Effect = effect;
           // //this.Effect = effect;

           // this.MainGrid.ContextMenu = contextMenu;
           //// initNotifyIcon();

           
           // this.menuCtrlMainWindow.Click += new RoutedEventHandler(menuCtrlMainWindow_Click);
           // this.menuCtrlSuspendWindow.Click += new RoutedEventHandler(menuCtrlSuspendWindow_Click);
           // this.menuCtrlApplication.Click += new RoutedEventHandler(menuCtrlApplication_Click);
           this.notifyIcon.MouseDoubleClick += new MouseButtonEventHandler(notifyIcon_MouseDoubleClick);

            initMenu();

           
            
        }

        void SuspendWindow_MouseMove(object sender, MouseEventArgs e)
        {
            //MessageBox.Show("SuspendWindow_MouseMove");
        }

        //void SuspendWindow_Unloaded(object sender, RoutedEventArgs e)
        //{
        //    MessageBox.Show("UnLoaded");
        //}

        

        private void initMenu()
        {
            menuSources = new MenuSource[]{
                new MenuSource("隐藏主窗体", new RoutedEventHandler(menuCtrlMainWindow_Click),null),
                new MenuSource("隐藏悬浮窗", new RoutedEventHandler(menuCtrlSuspendWindow_Click),null),
                new MenuSource("状态", new RoutedEventHandler(menuCtrlMainWindow_Click),null),
                new MenuSource("退出", new RoutedEventHandler(menuCtrlApplication_Click),null),
                new MenuSource("窗体状态", new RoutedEventHandler(menuWindowState_Click),null)
            };
            MenuItem[] menuItems = new MenuItem[menuSources.Length];

            for (int i = 0; i < menuSources.Length; i++)
            {
                menuItems[i] = menuSources[i].ToMenuItem();
            }
            this.MainGrid.ContextMenu = new ContextMenu();
            this.MainGrid.ContextMenu.ItemsSource = menuItems;
            this.notifyIcon.ContextMenu = this.MainGrid.ContextMenu;

        }

        void menuWindowState_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(string.Format("WindowState={0}\nVisiliy={1}",WindowState,Visibility));
        }

        void notifyIcon_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            
            parentWindow.InvokeIfNeeded(() =>
            {
                //parentWindow.ShowInTaskbar = true;

                parentWindow.WindowState = WindowState.Normal;
                //parentWindow.Show();
               // parentWindow.Activate();
                //parentWindow.Visibility = System.Windows.Visibility.Visible;
                                
               
            }, DispatcherPriority.Send);
            
           

        }
       

   
        
        public SuspendWindow(Window mainWindow)
            :this()
        {
            parentWindow = mainWindow;
            
        }
        public void a()
        {
            MessageBox.Show(this.Dispatcher.Thread.Name);            
            this.Show();
        }

        void menuCtrlApplication_Click(object sender, RoutedEventArgs e)
        {
            ExitApplication();
        }

        void menuCtrlSuspendWindow_Click(object sender, RoutedEventArgs e)
        {
            MenuItem tmp = sender as MenuItem;
            switch (this.Visibility)
            { 
                case System.Windows.Visibility.Visible:
                    this.Visibility = System.Windows.Visibility.Hidden;
                    if(tmp!=null)
                        tmp.Header = tmp.Header.ToString().Replace("隐藏", "显示");
                    break;
                case System.Windows.Visibility.Hidden:
                case System.Windows.Visibility.Collapsed:
                    this.Visibility = System.Windows.Visibility.Visible;
                    if(tmp!=null)
                        tmp.Header = tmp.Header.ToString().Replace("显示","隐藏");
                    break;
            }            
        }

        void menuCtrlMainWindow_Click(object sender, RoutedEventArgs e)
        {
            parentWindow.InvokeIfNeeded(() => parentWindow.WindowState = System.Windows.WindowState.Minimized, DispatcherPriority.Send);
        }

        /// <summary>
        /// 结束程序，跨线程让mainWindow结束程序
        /// </summary>
        public void ExitApplication()
        {
            HideNotifyIcon();
            parentWindow.InvokeIfNeeded(() => Application.Current.Shutdown(), DispatcherPriority.Send);
        }

        public void HideNotifyIcon()
        {
            notifyIcon.Visibility = System.Windows.Visibility.Hidden;
        }

       
    }

    
}
